package com.bcfrj.shop.controller;

import com.bcfrj.shop.pojo.Cart;
import com.bcfrj.shop.pojo.Good;
import com.bcfrj.shop.pojo.User;
import com.bcfrj.shop.service.impl.CartServiceImpl;
import com.bcfrj.shop.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/cart")
public class CartController {
    @Autowired
    private CartServiceImpl cartService;
    @GetMapping("/cartList")
    public Result getCartList(String User_account){
        List<Cart> cartList=null;
        if (User_account != null) {
            cartList = cartService.queryCartAll(User_account);
            if (cartList != null) {
                return Result.ok(cartList);
            }
            else {
                return Result.fail("购物车为空");
            }
        }
        else {
            return Result.fail("请登陆");
        }
    }

    @PostMapping("/addCart")
    public Result addCart(Cart cart,Integer Good_id,String User_account) {

        System.out.println(cart);
        if (cart!=null && Good_id!=null && User_account!=null){
            Good good = new Good();
            good.setGood_id(Good_id);
            User user = new User();
            user.setAccount(User_account);
            cart.setGood(good);
            cart.setUser(user);
            Integer rows = cartService.insertCart(cart);
            if (rows>0){
                return Result.ok("商品添加至购物车成功");
            }
            else {
                return Result.fail("商品添加至购物车失败");
            }
        }
        else{
            return Result.fail("商品添加至购物车失败");
        }
    }

    @PostMapping("/delCart")
    public Result delCart(Integer id){
        Integer rows = cartService.deleteCart(id);
        if (rows>0){
            return Result.ok("删除成功");
        }
        else {
            return Result.fail("删除失败");
        }


    }
}
